package com.gosling.cloudspace.common.entity;

import com.gosling.cloudspace.common.constants.CommonCodeEnum;
import lombok.Data;

import java.io.Serializable;

/**
 * 封装返回结果
 *
 * @email 1793327033@qq.com
 * @classname BaseResult
 * @author: osc_lihonglin
 * @date: 2021/6/23 22:06
 */
@Data
public class BaseResult<T> implements Serializable {
    //执行结果，true为执行成功 false为执行失败
    private boolean flag;
    //返回结果信息，主要用于页面提示信息
    private String message;
    //执行结果码
    private Integer code;
    //返回数据
    private T data;
    //业务调用唯一id
    private String traceId;
    //响应时间
    private String responseTime;

    public BaseResult() {
    }

    public BaseResult(boolean flag, Integer code, String message) {
        super();
        this.code = code;
        this.flag = flag;
        this.message = message;
    }

    public BaseResult(boolean flag, Integer code, String message, T data) {
        this.code = code;
        this.flag = flag;
        this.message = message;
        this.data = data;
    }

    /**
     * 为了便捷Result对象的使用,在次对象中提供静态方法,来快速创建出Result对象
     *
     * @param enums
     * @return
     */
    public static BaseResult success(CommonCodeEnum enums) {
        return new BaseResult(true, enums.getCode(), enums.getErrorMessage());
    }

    public static BaseResult success() {
        return new BaseResult(true, CommonCodeEnum.SUCCESS.getCode(), CommonCodeEnum.SUCCESS.getErrorMessage());
    }

    public static BaseResult success(String message) {
        return new BaseResult(true, CommonCodeEnum.SUCCESS.getCode(), message);
    }

    public static BaseResult success(Object obj) {
        return new BaseResult(true, CommonCodeEnum.SUCCESS.getCode(), CommonCodeEnum.SUCCESS.getErrorMessage(), obj);
    }

    public static BaseResult success(CommonCodeEnum enums, String message) {
        return new BaseResult(true, enums.getCode(), message);
    }

    public static BaseResult success(CommonCodeEnum enums, String message, Object data) {
        return new BaseResult(true, enums.getCode(), message, data);
    }

    public static BaseResult success(CommonCodeEnum enums, Object data) {
        return new BaseResult(true, enums.getCode(), enums.getErrorMessage(), data);
    }

    public static BaseResult failed(CommonCodeEnum enums) {
        return new BaseResult(false, enums.getCode(), enums.getErrorMessage());
    }

    public static BaseResult failed(CommonCodeEnum enums, String message) {
        return new BaseResult(false, enums.getCode(), message);
    }

    public static BaseResult failed(Integer code, String message) {
        return new BaseResult(false, code, message);
    }

    public static BaseResult failed(String message) {
        return new BaseResult(false, CommonCodeEnum.SERVER_ERROR.getCode(), message);
    }
}
